package com.JavaNet.chapter5_url;

/**
 * @author : wushikai
 * <p>
 * date : 2021-04-19
 *
 * 自己启动mq
 * 在窗口中输入: http://localhost:8161/admin/index.jsp
 *
 * 账号密码 都是  admin
 *
 * mq 的页面代码就会展示在窗口上 ;
 *
 */
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Application extends Frame {

    private TextField tf = new TextField();
    private TextArea ta = new TextArea();

    public Application() {

        super ("URL Password");

        // 安装 Authenticator
        Authenticator.setDefault (new MyAuthenticator ());

        // 设置屏幕
        add (tf, BorderLayout.NORTH);
        ta.setEditable(false);
        add (ta, BorderLayout.CENTER);
        tf.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                String s = tf.getText();
                if (s.length() != 0)
                    ta.setText (fetchURL (s));
            }
        });
        addWindowListener (new WindowAdapter() {
            public void windowClosing (WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });
    }

    private String fetchURL (String urlString) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try {
            URL url = new URL (urlString);
            InputStream content = (InputStream)url.getContent();
            BufferedReader in =
                    new BufferedReader (new InputStreamReader (content));
            String line;
            while ((line = in.readLine()) != null) {
                pw.println (line);
            }
        } catch (MalformedURLException e) {
            pw.println ("Invalid URL");
        } catch (IOException e) {
            pw.println ("Error reading URL");
        }
        return sw.toString();
    }



    class MyAuthenticator extends Authenticator {



        protected PasswordAuthentication getPasswordAuthentication() {
            final Dialog jd = new Dialog (Application.this, "Enter password", true);
            jd.setLayout (new GridLayout (0, 1));
            Label jl = new Label (getRequestingPrompt());
            jd.add (jl);
            TextField username = new TextField();
            username.setBackground (Color.lightGray);
            jd.add (username);
            TextField password = new TextField();
            password.setEchoChar ('*');
            password.setBackground (Color.lightGray);
            jd.add (password);
            Button jb = new Button ("OK");
            jd.add (jb);
            jb.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    jd.dispose();
                }
            });
            jd.pack();
            jd.setVisible(true);
            return new PasswordAuthentication (username.getText(), password.getText().toCharArray());
        }
    }


    public static void main (String args[]) {
        Frame f = new Application();
        f.setSize(400, 400);
        f.setVisible (true);
    }

}
